/**
 * Created by HUANGFR2 on 8/21/2016.
 */
angular.module('audit_dish', []).controller('audit_dishController', function ($scope, $rootScope, $http, $uibModal, DishService) {
    //console.log('audit_dish init');
    $rootScope.active = 'ad';
    $http.get('/dish')
        .then(function (res) {
            var data = res.data;
            if (data.msg == 'SUCCESS') {
                $scope.dishs = data.dishs;
            } else {
                alert('get dish error : ' + data.error);
            }
        }, function (res) {
            alert('get dish error : ' + res.error);
        });

    $scope.updateStatus = function (dish, status) {
        DishService.updateDishStatus(dish, status);
    };

    $scope.openWarning = function (dish) {
        var modalInstance = $uibModal.open({
            animation: true,
            ariaLabelledBy: 'modal-title',
            ariaDescribedBy: 'modal-body',
            templateUrl: 'dishWarning_modal.html',
            controller: 'dishWarningModalController',
            size: 'sm',
            resolve: {
                parentScope: function () {
                    return $scope
                },
                dish: function () {
                    return dish
                }
            }
        });
    };
});

angular.module('audit_dish').controller('dishWarningModalController', function ($scope, $http, $uibModalInstance, parentScope, dish) {

    $scope.dish = dish;
    $scope.confirm = function () {
        parentScope.updateStatus(dish, 2);
        $scope.cancel();
    };

    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };

});

angular.module('audit_dish').filter('convertDishStatus', function () {
    return function (status) {
        switch (status) {
            case 0 :
                return 'Pending audit';
            case 1 :
                return 'Audit pass';
            case 2 :
                return 'Audit Refuse';
        }
    }
});